package com.example.myapplication.tools;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.rightin.reader.hf.model.ReadBlocksResult;
import com.rightin.reader.hf.myhf.HfReader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RfidUtils {
    public static final int ERROR_EXCEPTION = -2;
    public static final int ERROR_NOT_READ = -1;
    public static final int ERROR_NOT_MATCHING = 0;
    public static final int ERROR_OPEN_FAIL = 1;
    public static final int ERROR_WRITE_NOT_FIND_RFID = 2;
    public static final int WRITE_SUCCESS = 3;
    public static final String NOT_WRITE_TAG_CONTAINSKEY = "未写入";

    HfReader reader;
    List<String> uidList;
    Map<String,String> mapResult;
    String lastContent = "";
    boolean isRun;
    int index;
    Handler handlerCallBack;

    public RfidUtils(HfReader _reader){
        reader = _reader;
        uidList = new ArrayList<>();
        mapResult = new HashMap<>();
        index = 0;
        isRun = false;
    }

    /**
     * 读取RFID标签内容 国标
     * @param uid
     * @return
     */
    public String readRfidData(String uid){
        String content = readRFIDContent(uid, 5);
        String result = RFIDWriteReadUtile.readRfidDataByHex(content);
        if(result.length() == 0){
            return "";
        }else{
            return result;
        }
    }

    /**
     * 读取RFID标签内容 国标
     * @param uid
     * @return
     */
    public void readRfidData(String uid, Handler handler){
        if(handlerCallBack == null){
            handlerCallBack = handler;
        }
        if(!uidList.contains(uid)){
            uidList.add(uid);
            selectContent();
        }
    }
    private void selectContent(){
        new Thread(){
            @Override
            public void run() {
                if(!isRun){
                    if(uidList.size() > index){
                        isRun = true;
                        String uid = uidList.get(index);
                        String content = readRFIDContent(uid, 5);
                        String result = RFIDWriteReadUtile.readRfidDataByHex(content);
                        if(result.length() > 0 && !lastContent.equals(result)){
                            lastContent = result;
                            Message msg = new Message();
                            msg.obj = new String[]{uid,result};
                            handlerCallBack.sendMessage(msg);
                            index++;
                        }
                        isRun = false;
                        selectContent();
                    }

                }
            }
        }.start();
    }

    private String readRfidContent(String uid){
        String content = readRFIDContent(uid, 5);
        String result = RFIDWriteReadUtile.readRfidDataByHex(content);
        if(result.length() == 0){
            return "";
        }else{
            return result;
        }
    }


    /**
     * 写入数据，没有限制长度，原则上不超过
     * @param content
     */
    public boolean writeRFIDData(String uid,String content){
        ArrayList<String> block_dataList = new ArrayList<>();
        String temp = RFIDWriteReadUtile.getWriteRfidDataByStr(content);
        getRFIDData(temp,block_dataList);
        boolean isSuccess = false;
        for(int i = 0 ; i < block_dataList.size() ; i ++){
            ArrayList<String> listTemp = new ArrayList<>();
            listTemp.add(block_dataList.get(i));
            isSuccess = reader.writeMultipleBlocks(uid,i , listTemp);
        }
        return isSuccess;
    }

    /**
     * 通过UID和读取块数读取内容 String
     * @param uid
     * @param blockNum
     * @return
     */
    public String readRFIDContent(String uid,int blockNum){
        ReadBlocksResult.dataList = null;
        boolean isOk = reader.readMultipleBlocks(uid, 0, blockNum);
//        if(isOk){//4106170C30C30E786F020131    4106313C34D71E72
            StringBuilder sb = new StringBuilder();
            if(ReadBlocksResult.dataList == null){
                Log.e("---->>> ", "uid: 获取datalist 为null");
                return "";
            }
            for(String str : ReadBlocksResult.dataList){
                sb.append(str);
            }
            String temp = sb.toString();
            if(temp.length() == 0){
                return "";
            }
            return temp;
//        }else{
//            return "";
//        }
    }

    /**
     * 获取写入的barcode字符串   每条list不够8为按F补齐
     * @param str
     * @param list
     */
    public void getRFIDData(String str , ArrayList<String> list){
        if(list == null){
            list = new ArrayList<>();
        }else{
            list.clear();
        }
        str = str.toUpperCase();
        if(str.length() > 8){
            int _blocks = str.length() / 8;
            int end = str.length() % 8;
            for(int i = 0 ; i < _blocks ; i ++){
                int start = i*8;
                String tempStr = str.substring(start,start + 8);
                list.add(tempStr);
            }
            if(end > 0){
                String tempStr = str.substring(_blocks * 8);
                for(int i = 0;i < 8-end ; i ++){
                    tempStr += "F";
                }
                list.add(tempStr);
            }
        }else if(str.length() == 8){
            list.add(str);
        }else{
            String tempStr = str;
            for(int i = 0;i < 8 - str.length() ; i ++){
                tempStr += "F";
            }
            list.add(tempStr);
        }
    }

}
